﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.MathUtilitys;
using Microsoft.Xna.Framework;

namespace Utils.Layouting
{
    public class BasePlane
    {
        protected FRect rect;
        protected Vector2 maxSize;
        protected float marginLeft;
        protected float marginRight;
        protected float marginTop;
        protected float marginBottom;
        protected BasePlane child;

        public FRect Rect { get { return rect; } set { rect = value; } }
        public Vector2 MaxSize { get { return maxSize; } set { maxSize = value; } }
        public float MaxWidth { get { return maxSize.X; } set { maxSize.X = value; } }
        public float MaxHeight { get { return maxSize.Y; } set { maxSize.Y = value; } }
        public float Margin { set { marginLeft = marginRight = marginTop = marginBottom = value; } }
        public float MarginLeft { set { marginLeft = value; } }
        public float MarginRight { set { marginRight = value; } }
        public float MarginTop { set { marginTop = value; } }
        public float MarginBottom { set { marginBottom = value; } }
        public BasePlane Child { get { return child; } set { child = value; } }

        public BasePlane()
        {
            rect = new FRect();
        }

        public BasePlane(FRect rect)
        {
            this.rect = rect;
        }

        public virtual void UpdateSize(FRect newRect)
        {
            float newWidth = newRect.Width - marginLeft - marginRight;
            float newHeight = newRect.Height - marginTop - marginBottom;
            if(maxSize.X != 0)
            {
                newWidth = MathUtils.Min(newWidth, maxSize.X);
            }
            if (maxSize.Y != 0)
            {
                newHeight = MathUtils.Min(newHeight, maxSize.Y);
            }
            this.rect = new FRect(newRect.X + (marginLeft - marginRight) / 2f,
                                newRect.Y + (marginTop - marginBottom) / 2f,
                                newWidth, newHeight);

            if (child != null)
            {
                child.UpdateSize(rect);
            }
        }
    }
}
